function ContactAdd_Code(id) {
	var buffer = new StringBuffer();
	buffer.append('<div class="crmsheet border-bottom"><div class="ctitle"><div class="cname">联系人</div><div class="require">*</div></div><div class="ctext"><input id="txt_name" type="text" placeholder="填写联系人名字" value=""/></div><div class="carrow"></div></div>');
	buffer.append('<div class="crmsheet border-bottom"><div class="ctitle"><div class="cname">手机</div></div><div class="ctext"><input id="txt_mobile" type="tel" maxlength="11" placeholder="填写手机" value=""/></div><div class="carrow"></div></div>');
	buffer.append('<div class="crmsheet border-bottom"><div class="ctitle"><div class="cname">固话</div></div><div class="ctext"><input id="txt_phone" type="tel" placeholder="填写固话" value=""/></div><div id="position" class="carrow"></div></div>');
	buffer.append('<div class="crmsheet border-bottom"><div class="ctitle"><div class="cname">性别</div></div><div class="ctext"><div class="sex"><input id="boy" name="sex" type="radio" style="width:16px;" checked="checked" value="男"/><label for="boy">男</label></div><div class="sex"><input id="girls" name="sex" type="radio" style="width:16px;" value="女"/><label for="girls">女</label></div></div></div>');
	buffer.append('<div class="crmsheet border-bottom"><div class="ctitle"><div class="cname">Email</div></div><div class="ctext"><input id="txt_email" type="text" placeholder="填写Email" value="" onfocus="ScrollTop()"/></div><div class="carrow"></div></div>');
	buffer.append('<div class="crmsheet border-bottom"><div class="ctitle"><div class="cname">QQ</div></div><div class="ctext"><input id="txt_qq" type="tel" placeholder="填写QQ" value="" onfocus="ScrollTop()"/></div><div class="carrow"></div></div>');
	buffer.append('<div class="crmsheet border-bottom"><div class="ctitle"><div class="cname">公司名称</div></div><div class="ctext"><input id="txt_company" type="text" placeholder="填写公司名称" value="" onfocus="ScrollTop()"/></div><div class="carrow"></div></div>');
	buffer.append('<div class="crmsheet border-bottom"><div class="ctitle"><div class="cname">联系地址</div></div><div class="ctext"><input id="txt_address" type="text" placeholder="填写联系地址" value="" onfocus="ScrollTop()"/></div><div class="carrow"></div></div>');
	buffer.append('<div class="crmsheet border-bottom"><div class="ctitle"><div class="cname">职务</div></div><div class="ctext"><input id="txt_duty" type="text" placeholder="填写职务" value="" onfocus="ScrollTop()"/></div><div class="carrow"></div></div>');
	buffer.append('<div class="crmsheet border-bottom"><div class="ctitle"><div class="cname">部门</div></div><div class="ctext"><input id="txt_depart" type="text" placeholder="填写部门" value="" onfocus="ScrollTop()"/></div><div class="carrow"></div></div>');
	buffer.append('<div class="crmsheet border-bottom"><div class="ctitle"><div class="cname">角色关系</div><div class="require">*</div></div><div class="ctext"><select id="txt_relationship"></select></div><div class="carrow"><img src="img/arrow.png"/></div></div>');
	buffer.append('<div class="crmsheet border-bottom"><div class="ctitle"><div class="cname">亲密程度</div><div class="require">*</div></div><div class="ctext"><select id="txt_degree"></select></div><div class="carrow"><img src="img/arrow.png"/></div></div>');
	
	buffer.append('<div id="selfcontent" val="'+id+'" customerid="0">');
	buffer.append('</div>');
	buffer.append('<div class="crmsheet border-bottom" style="height:120px;"><div class="ctitle"><div class="cname">备注</div></div><textarea id="txt_bak" placeholder="填写备注" onfocus="ScrollTop()"></textarea></div>');
	buffer.append('<div id="contact_tool" class="cline border-bottom"><font>展开更多</font></div>');
	buffer.append('<div id="contact_panel">');
	buffer.append('<div class="crmsheet border-bottom"><div class="ctitle"><div class="cname">微信</div></div><div class="ctext"><input id="txt_weixin" type="text" placeholder="填写微信" value="" onfocus="ScrollTop()"/></div></div>');
	buffer.append('<div class="crmsheet border-bottom"><div class="ctitle"><div class="cname">微博</div></div><div class="ctext"><input id="txt_weibo" type="text" placeholder="填写微博" value="" onfocus="ScrollTop()"/></div></div>');
	buffer.append('<div class="crmsheet border-bottom"><div class="ctitle"><div class="cname">生日</div></div><div class="ctext"><input id="txt_birthday" type="text" placeholder="填写生日" value="" onfocus="ScrollTop()"/></div></div>');
	buffer.append('<div class="crmsheet border-bottom"><div class="ctitle"><div class="cname">爱好</div></div><div class="ctext"><input id="txt_hobby" type="text" placeholder="填写爱好" value="" onfocus="ScrollTop()"/></div></div>');
	buffer.append('<div class="crmsheet border-bottom"><div class="ctitle"><div class="cname">网址</div></div><div class="ctext"><input id="txt_website" type="text" placeholder="填写网址" value="" onfocus="ScrollTop()"/></div></div>'); 
	buffer.append('<div class="crmsheet border-bottom"><div class="ctitle"><div class="cname">传真</div></div><div class="ctext"><input id="txt_fax" type="text" placeholder="填写传真" value="" onfocus="ScrollTop()"/></div></div>');
	buffer.append('</div>');
	buffer.append('<div style="width:100%; float:left; height:100px;"></div>');
	
	return buffer.toString();
};

var map;
var wsub;
var count=0;
function Init_ContactAdd(customerid){
	$("#selfcontent").attr("customerid",customerid);
	var contactid=$("#selfcontent").attr("val");
		
		get("contact_tool").addEventListener("tap",function(){
			var state = $(this).find("font").html();
			if(state=="展开更多")
			{
				$("#contact_panel").show();
				$(this).find("font").html("关闭更多");
			}else
			{
				$("#contact_panel").hide();	
				$(this).find("font").html("展开更多");
			}
		});
	

	var customer;
	if(contactid!=0)
	{
		var data = {};
		data.act = "One_Contact";
		data.id=contactid;
		data.source=source;
		data.userid=userid;
		data.comid=comid;
		data.sign=GetSign(data.act);
		mui.getJSON(crm_domain, data, function (json) {
	            if (json.state) {
	                contact=json.data.contact;
	                $("#txt_name").val(contact.Name);
			        $("#txt_mobile").val(contact.Mobile);
			        $("#txt_phone").val(contact.Phone);
			        $("#txt_relationship").val(contact.RelationShip);
			        $("#txt_degree").val(contact.Degree);
			        $("input[name='sex'][value='"+contact.Sex+"']").prop("checked", true);
			        $("#txt_duty").val(contact.Duty);
			        $("#txt_depart").val(contact.Depart);
			        $("#txt_company").val(contact.Company);
			        $("#txt_website").val(contact.WebSite);
			        $("#txt_birthday").val(contact.Birthday);
			        $("#txt_hobby").val(contact.Hobby);
			        $("#txt_address").val(contact.Address);
			        $("#txt_email").val(contact.Email);
			        $("#txt_fax").val(contact.Fax);
			        $("#txt_weixin").val(contact.WeiXin);
			        $("#txt_weibo").val(contact.WeiBo);
			        $("#txt_qq").val(contact.QQ);
			        $("#txt_bak").val(contact.Bak);
	                Init_ContactTag(contact); 
	            } else {
					plus.nativeUI.toast(json.msg); 
	            }
	    });
    }else{
		Init_ContactTag(); //初始化自定义Tag
	}
};

function Init_ContactTag(customer)
{
	var buffer = new StringBuffer();
	var data = {};
	data.act = "Contact_Tag";
	data.userid=userid;
	data.comid=comid;
	data.sign=GetSign(data.act);
	mui.getJSON(crm_domain, data, function(json) {
		$.each(json.data.taglist,function(i,info){
			if (info.Type == 1)
            {
			buffer.append('<div class="crmsheet border-bottom"><div class="ctitle"><div class="cname">'+info.Name+'</div>');
			if(info.IsRequired){
				buffer.append('<div class="require">*</div>');
			}
			buffer.append('</div><div class="ctext">');
			buffer.append('<select name="" class="select_new selftag" val="'+info.ColumnNum+'">');
                buffer.append('<option value="">选择'+info.Name+'</option>');
                $.each(info.TagDetail,function(k,detail)
                {
                	var isselect="";
                	if(customer){
                    if (info.ColumnNum == 1) { if (customer.Column1 == detail.Name) { isselect = "selected='selected'"; } else { isselect = ""; } }
                    if (info.ColumnNum == 2) { if (customer.Column2 == detail.Name) { isselect = "selected='selected'"; } else { isselect = ""; } }
                    if (info.ColumnNum == 3) { if (customer.Column3 == detail.Name) { isselect = "selected='selected'"; } else { isselect = ""; } }
                    if (info.ColumnNum == 4) { if (customer.Column4 == detail.Name) { isselect = "selected='selected'"; } else { isselect = ""; } }
                    if (info.ColumnNum == 5) { if (customer.Column5 == detail.Name) { isselect = "selected='selected'"; } else { isselect = ""; } }
                    if (info.ColumnNum == 6) { if (customer.Column6 == detail.Name) { isselect = "selected='selected'"; } else { isselect = ""; } }
                    if (info.ColumnNum == 7) { if (customer.Column7 == detail.Name) { isselect = "selected='selected'"; } else { isselect = ""; } }
                    if (info.ColumnNum == 8) { if (customer.Column8 == detail.Name) { isselect = "selected='selected'"; } else { isselect = ""; } }
                    if (info.ColumnNum == 9) { if (customer.Column9 == detail.Name) { isselect = "selected='selected'"; } else { isselect = ""; } }
                    if (info.ColumnNum == 10) { if (customer.Column10 == detail.Name) { isselect = "selected='selected'"; } else { isselect = ""; } }
                   }
                	buffer.append('<option value="'+detail.Name+'" '+isselect +'>'+detail.Name+'</option>');
                });
                buffer.append('</select>');
			buffer.append('</div><div class="carrow"><img src="img/arrow.png"/></div></div>');
			}else{
			  var selftext = "";
			  if(customer){
              if (info.ColumnNum == 1) { selftext = customer.Column1; }
              if (info.ColumnNum == 2) { selftext = customer.Column2; }
              if (info.ColumnNum == 3) { selftext = customer.Column3; }
              if (info.ColumnNum == 4) { selftext = customer.Column4; }
              if (info.ColumnNum == 5) { selftext = customer.Column5; }
              if (info.ColumnNum == 6) { selftext = customer.Column6; }
              if (info.ColumnNum == 7) { selftext = customer.Column7; }
              if (info.ColumnNum == 8) { selftext = customer.Column8; }
              if (info.ColumnNum == 9) { selftext = customer.Column9; }
              if (info.ColumnNum == 10) { selftext = customer.Column10; }
              }
              buffer.append('<div class="crmsheet border-bottom"><div class="ctitle"><div class="cname">'+info.Name+'</div>');
              if(info.IsRequired){
              buffer.append('<div class="require">*</div>');
              }
              buffer.append('</div><div class="ctext"><input id="txt_name" type="text" class="selftag" placeholder="填写'+info.Name+'" value="'+selftext+'" val="'+info.ColumnNum+'" onfocus="ScrollTop()"/></div></div>');
			}
		});
		$("#selfcontent").html(buffer.toString());
		
		buffer = new StringBuffer();
		$.each(json.data.relationship,function(i,info){
			buffer.append('<option value="'+info.Name +'">'+info.Name+'</option>');
		});
		$("#txt_relationship").html(buffer.toString());
		
		buffer = new StringBuffer();
		$.each(json.data.degree,function(i,info){
			buffer.append('<option value="'+info.Name +'">'+info.Name+'</option>');
		});
		$("#txt_degree").html(buffer.toString());
		if(customer){
			$("#txt_relationship").val(customer.RelationShip);
			$("#txt_degree").val(customer.Degree);
		}

	});
};


function Save_Contact() {
        var data = {};
        var txt_name = $("#txt_name");
        data.mobile = $("#txt_mobile").val();
        data.phone = $("#txt_phone").val();
        if(txt_name.val().Trim()=="")
        {
        	plus.nativeUI.toast("请填写联系人姓名");
        	return false;
        }
        if(data.mobile.Trim()=="" && data.phone.Trim()=="" )
        {
        	plus.nativeUI.toast("手机和固话至少填写1项");
        	return false;
        }
		var error = false;
        $("#selfcontent .require").parent().parent().find(".selftag").each(function () {
            var tagname = this.tagName.toLowerCase();
            if ($(this).val().Trim()=="") {
            	
                if (tagname == "input") {
                    plus.nativeUI.toast($(this).attr("placeholder"));
                } else {
                    plus.nativeUI.toast("请" + $(this).find("option:first").html());
                }
                error = true;
                return false;
            }
        });
        if (error) {
            return;
        }

        data.act = "Save_Contact";
        data.id = $("#selfcontent").attr("val");
        data.customerid=$("#selfcontent").attr("customerid");
        data.userid=userid;
		data.comid=comid;
        data.name = $("#txt_name").val();
        data.relationship = $("#txt_relationship").val();
        data.degree = $("#txt_degree").val();
        data.sex = $("input[name='sex']:checked").val();
        data.duty = $("#txt_duty").val();
        data.depart = $("#txt_depart").val();
        data.company = $("#txt_company").val();
        data.website = $("#txt_website").val();
        data.birthday = $("#txt_birthday").val();
        data.hobby = $("#txt_hobby").val();
        data.address = $("#txt_address").val();
        data.email = $("#txt_email").val();
        data.fax = $("#txt_fax").val();
        data.weixin = $("#txt_weixin").val();
        data.weibo = $("#txt_weibo").val();
        data.qq = $("#txt_qq").val();
        data.bak = $("#txt_bak").val();
        data.source = source; 
        data.version = version;
        var selftag = new Array();
        $(".selftag").each(function () {
            selftag.push($(this).attr("val") + "*" + $(this).val());
        });
        data.selftag = selftag + "";
        data.sign=GetSign(data.act);
        mui.getJSON(crm_domain, data, function (json) {
            if (json.state) {
                CloseWindow();
                plus.nativeUI.toast(json.msg);
                if(data.id==0)
                {
                	if(data.customerid==0)
                	{
	                	Clear_Form();
	                	Contact_Search();
	                	FindCrmReport();
                	}else
                	{
                		Receive_ContactInfo();
                	}
                }else{
                	Init_ContactDetail();
                	obj.find(".customername").html(data.name);
                	var mobile=data.mobile;
                	if(mobile.Trim()==""){mobile=data.phone;}
                	obj.find(".state").html(mobile);
                }
            } else {
				plus.nativeUI.toast(json.msg); 
            }
        });
    };
